package com.aboo.user.action;

import com.aboo.frame.vo.ReturnVO;
import com.aboo.user.entity.vo.UserVO;
import com.aboo.user.service.UserService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/user")
public class UserAction {

    @Resource
    private UserService userService;

    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        return "/user/list";
    }

    @RequestMapping("/add")
    public String add(HttpServletRequest request) {
        return "/user/add";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public ReturnVO<List<UserVO>> listData(HttpServletRequest request) {
        ReturnVO<List<UserVO>> rt = new ReturnVO<>();

        List<UserVO> list = new ArrayList<>(16);
        list.add(new UserVO("张三", "26", "000021"));
        list.add(new UserVO("李四", "28", "000022"));
        list.add(new UserVO("赵六", "34", "000023"));
        rt.setData(list);

        return rt;
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public ReturnVO<UserVO> addUser(HttpServletRequest request) {
        ReturnVO<UserVO> rt = new ReturnVO<>();
        String userName = request.getParameter("userName");
        String userAge = request.getParameter("userAge");
        String userCard = request.getParameter("userCard");
        UserVO user = new UserVO(userName, userAge, userCard);

        UserVO data = userService.addUser(user);
        rt.setData(data);
        log.info(JSONObject.toJSONString(data));

        return rt;
    }

}
